package com.example.administrator.newsapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.administrator.newsapp.R;
import com.example.administrator.newsapp.pojo.Comment;

import java.util.List;

/**
 * ClassName:
 * Description:
 */

public class ListCommentAdapter extends BaseAdapter {
    private List<Comment> mCommentList;
    private Context mContext;

    public ListCommentAdapter(List<Comment> commentList, Context context) {
        mCommentList = commentList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mCommentList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCommentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.comment_list_item,parent,false);
            MyHolder myHolder = new MyHolder();
            myHolder.username = convertView.findViewById(R.id.item_list_username);
            myHolder.comment=convertView.findViewById(R.id.item_list_comment);
            myHolder.zanNum=convertView.findViewById(R.id.tv_zan_num);
            myHolder.ibZan=convertView.findViewById(R.id.ib_dianzan);
            myHolder.time=convertView.findViewById(R.id.item_list_time);
            convertView.setTag(myHolder);
        }

        MyHolder myHolder = (MyHolder) convertView.getTag();
        Comment c = mCommentList.get(position);
        myHolder.username.setText(c.getUser_id()+": ");
        myHolder.comment.setText(c.getComment_content());
        if(String.valueOf(c.getZanNum()).equals("")){
            c.setZanNum(0);
        }
        myHolder.zanNum.setText(c.getZanNum()+"");
        myHolder.time.setText(c.getCreatedAt()+"");
        myHolder.ibZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemZanListener.onZanClick(position);
            }
        });
        return convertView;
    }
    class MyHolder{
        private TextView username;
        private TextView comment;
        private TextView time;
        private TextView zanNum;
        private ImageButton ibZan;
    }
    public interface onItemZanListener{
        void onZanClick(int i);
    }

    private onItemZanListener mOnItemZanListener;

    public void setOnItemZanClickListener(onItemZanListener mOnItemZanListener) {
        this.mOnItemZanListener = mOnItemZanListener;
    }
}
